﻿using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace HECore.MainCore.Extensions
{
    public static class SwaggerExtensions
    {
        /// <summary>
        /// 框架扩展：使用Swagger
        /// </summary>
        /// <param name="app"></param>
        /// <returns></returns>
        public static IApplicationBuilder UseSwaggerUI(this IApplicationBuilder app)
        {
            var Configuration = (IConfiguration)app.ApplicationServices.GetService(typeof(IConfiguration));

            bool IsEnable = bool.Parse(Configuration["Swagger:IsEnable"]);
            if (IsEnable)
            {
                app.UseSwagger();

                app.UseSwaggerUI(option =>
                {
                    option.SwaggerEndpoint("/swagger/v1/swagger.json", "v1");
                });
            }
            return app;
        }

        /// <summary>
        /// 框架扩展：注册使用Swagger
        /// </summary>
        /// <param name="services"></param>
        /// <returns></returns>
        public static IServiceCollection AddSwaggerGen(this IServiceCollection services)
        {
            var configuration = services.BuildServiceProvider().GetService<IConfiguration>();

            bool IsEnable = bool.Parse(configuration["Swagger:IsEnable"]);
            if (IsEnable)
            {
                string Title = configuration["Swagger:Title"];
                string Description = configuration["Swagger:Description"];
                services.AddSwaggerGen(option =>
                {
                    option.SwaggerDoc("v1", new OpenApiInfo
                    {
                        Version = "v1",
                        Title = Title,
                        Description = $"{Title}<br/>{Description}"
                    });

                    OpenApiSecurityScheme scheme = new OpenApiSecurityScheme()
                    {
                        Description = "请输入token",
                        Reference = new OpenApiReference { Type = ReferenceType.SecurityScheme, Id = "token" },
                        Scheme = "oauth2",
                        Name = "token",
                        In = ParameterLocation.Header,
                        Type = SecuritySchemeType.ApiKey,
                    };
                    option.AddSecurityDefinition("token", scheme);
                    OpenApiSecurityRequirement requirement = new OpenApiSecurityRequirement();
                    requirement[scheme] = new List<string>();
                    option.AddSecurityRequirement(requirement);

                    var XMLFiles = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory).Where(x => x.Contains(".xml"));
                    foreach (var XMLFile in XMLFiles)
                    {
                        option.IncludeXmlComments(XMLFile, true);
                    }
                });
            }
            return services;
        }
    }
}
